package com.hdaccp.ch06
import org.apache.spark.SparkConf
import org.apache.spark.streaming.kafka.KafkaUtils
import org.apache.spark.streaming.{Seconds, StreamingContext}
/**
  * Spark Streaming对接Kafka
  */
object KafkaStreamingApp {
  def main(args: Array[String]): Unit = {

    /**
      * 192.168.15.41:2181 test-consumer-group testflume3,testflume1 1
      */
    if(args.length != 4) {
      System.err.println("Usage: KafkaStreamingApp <zkQuorum> <group> <topics> <numThreads>")
    }

   val Array(zkQuorum, group, topics, numThreads) = args

    //1.spark的配置信息
    val sparkConf = new SparkConf().setAppName("KafkaReceiverWordCount")
      .setMaster("local[2]")
    //2.spark streaming 上下文
    val ssc = new StreamingContext(sparkConf, Seconds(5))
   //3.
    val topicMap = topics.split(",").map((_, numThreads.toInt)).toMap


    val messages = KafkaUtils.createStream(ssc, zkQuorum, group,topicMap)


    //messages.map(_._2).count().print()

    messages.map(_._2).print

    ssc.start()
    ssc.awaitTermination()
  }
}
